<?php

/**
 * This is the model class for table "usuario".
 *
 * The followings are the available columns in table 'usuario':
 * @property integer $id
 * @property string $nombre
 * @property string $password
 * @property string $correo
 * @property integer $activo
 *
 * The followings are the available model relations:
 * @property Experimento[] $experimentos
 * @property Proyecto[] $proyectos
 * @property Proyecto[] $proyectos1
 */
class Usuario extends CActiveRecord
{
	public $password_repeat;
    
        /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Usuario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre, password, correo, password_repeat', 'required'),
			array('activo', 'numerical', 'integerOnly'=>true),
			array('nombre, correo', 'length', 'max'=>255),
			array('password', 'length', 'max'=>20),
                        array('password', 'length', 'min'=>4),
                        array('correo', 'email'),
                        array('correo, nombre', 'unique'),
                        array('password', 'compare'),
                        array('password_repeat', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, nombre, correo, activo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'experimentos' => array(self::HAS_MANY, 'Experimento', 'idInvestigador'),
			'proyectos' => array(self::HAS_MANY, 'Proyecto', 'idCoordinador'),
			'proyectos1' => array(self::MANY_MANY, 'Proyecto', 'usuarioxproyecto(idUsuario, idProyecto)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nombre' => 'Name',
			'password' => 'Password',
                        'password_repeat' => 'Repeat Password',
			'correo' => 'Email',
			'activo' => 'Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nombre',$this->nombre,true);
		//$criteria->compare('password',$this->password,true);
		$criteria->compare('correo',$this->correo,true);
		$criteria->compare('activo',$this->activo);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function getActivoText()
        {
            if($this->activo)
                return "Si";
            else
                return "No";
        }
        
        
        /**
        * apply a hash on the password before we store it in the database
        */
        protected function afterValidate()
        {
            parent::afterValidate();
            if(!$this->hasErrors())
            $this->password = $this->hashPassword($this->password);
        }
        
        
        /**
        * Generates the password hash.
        * @param string password
        * @return string hash
        */
        public function hashPassword($password)
        {
            return md5($password);
        }
        
        /**
        * Checks if the given password is correct.
        * @param string the password to be validated
        * @return boolean whether the password is valid
        */
        public function validatePassword($password)
        {
            return $this->hashPassword($password)===$this->password;
            //return true;
        }
}